package com.hzeng.util;

import java.util.ArrayList;
import java.util.List;

import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;

public class ProgramTableModel implements TableModel{
	private List<Program> programList;
	private int columnCount = 0;
	
	public ProgramTableModel() {
		if (programList == null) {
			programList = new ArrayList<Program>();
		}
	}
	
	public void setColumnCount(int columnCount) {
		this.columnCount = columnCount;
	}

	public List<Program> getProgramList() {
		return programList;
	}

	public void setProgramList(List<Program> programList) {
		this.programList = programList;
	}

	@Override
	public void addTableModelListener(TableModelListener l) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void removeTableModelListener(TableModelListener l) {
		// TODO Auto-generated method stub
		
	}
	
	public Program getRow(int selectRow) {
		if(programList == null || programList.size() < 1) {
			return null;
		}
		
		int rowCount = getRowCount();
		
		if (selectRow < rowCount && selectRow >= 0) {
			return programList.get(selectRow);
		}else {
			return null;
		}
	}
	
	public void removeRow(int selectRow) {
		if(programList == null || programList.size() < 1) {
			return;
		}
		
		int rowCount = getRowCount();
		if (selectRow < rowCount && selectRow >= 0) {
			programList.remove(selectRow);
		}else {
			return;
		}
	}
	
	public void addRow(Program p) {
		programList.add(p);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Class<?> getColumnClass(int columnIndex) {
		
		Class colClass;  
        if ((columnIndex >= 0) && (columnIndex < getColumnCount())) {  
            colClass = getValueAt(-2, columnIndex).getClass();  
        } else {  
            colClass = Object.class;  
        }  
        return colClass;
	}

	@Override
	public int getColumnCount() {
		/*
	String name;
	long pid;
	double memory;
	double virtualMemory;
	double cpu;
	String directoryName;
	double read;
	double write;
		 */
		return columnCount;
	}

	@Override
	public String getColumnName(int columnIndex) {
		String columnName = null;
		switch (columnIndex) {
			case 0:
				//name
				columnName = Constants._NAME;
				break;
			case 1:
				//pid
				columnName = Constants._PID;
				break;
			case 2:
				//memory
				columnName = Constants._MEMORY;
				break;
			case 3:
				//virtual Memory
				columnName = Constants._VM;
				break;
				
			case 4:
				//cpu
				columnName = Constants._CPU;
				break;
			case 5:
				//read
				columnName = Constants._READ;
				break;
			case 6:
				//write
				columnName = Constants._WRITE;
				break;
			case 7:
				//directory Name
				columnName = Constants._DIRECTORY;
				break;
			case 8:
				//total Memory
				columnName = Constants._TOTAL_MEMORY;
				break;
			default:
				columnName = null;
				break;
		}
		return columnName;
	}

	@Override
	public int getRowCount() {
		if (programList != null) {
			return programList.size();
		}
		return 0;
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		if(programList == null || programList.size() < 1) {
			return null;
		}
		
		
		
		Program p = null;
		
		int rowCount = getRowCount();
		int columnCount = getColumnCount();
		
		if (rowIndex == -2) {
			p = new Program();
			p.setName("");
			p.setPid(0l);
			p.setMemory(0d);
			p.setVirtualMemory(0d);
			p.setCpu(0d);
			p.setRead(0d);
			p.setWrite(0d);
			p.setDirectoryName("");
			p.setTotalMemory(0d);
		}else if (rowIndex < rowCount && columnIndex < columnCount) {
			p = programList.get(rowIndex);
		}
		
		if (p == null) {
			return null;
		}
		switch (columnIndex) {
			case 0:
				//name
				return p.getName();
			case 1:
				//pid
				return p.getPid();
			case 2:
				//memory
				return p.getMemory();
			case 3:
				//virtual Memory
				return p.getVirtualMemory();
			case 4:
				//cpu
				return p.getCpu();
			case 5:
				//read
				return p.getRead();
			case 6:
				//write
				return p.getWrite();
			case 7:
				//directory Name
				return p.getDirectoryName();
			case 8:
				//total Memory
				return p.getTotalMemory();
			default:
				return null;
		}
	}

	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		
		return false;
	}

	

	@Override
	public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
		if (rowIndex < getRowCount() && columnIndex < getColumnCount()) {
			Program p = programList.get(rowIndex);
			switch (columnIndex) {
				case 0:
					//name
					p.setName((String) aValue);
					break;
				case 1:
					//pid
					p.setPid((Long) aValue);
					break;
				case 2:
					//memory
					p.setMemory((Double) aValue);
					break;
				case 3:
					//virtual Memory
					p.setVirtualMemory((Double) aValue);
					break;
				case 4:
					//cpu
					p.setCpu((Double) aValue);
					break;
				case 5:
					//read
					p.setRead((Double) aValue);
					break;
				case 6:
					//write
					p.setWrite((Double) aValue);
					break;
				case 7:
					//directory Name
					p.setDirectoryName((String) aValue);
					break;
				case 8:
					//total Memory
					p.setTotalMemory((Double) aValue);
					break;
				default:
					
					break;
			}
		}
	}

}
